﻿
using System;
using System.Diagnostics;
using My.Ioc;

namespace ServiceUpdate
{
    #region Test Types

    public interface IService
    {
        string Name { get; }
    }

    public abstract class Service : IService
    {
        public string Name
        {
            get { return GetType().Name; }
        }
    }

    public class Service1 : Service
    {
    }

    public class Service2 : Service
    {
    }

    public interface IServiceConsumer
    {
        IService Service { get; }
    }

    public class ServiceConsumer : IServiceConsumer
    {
        readonly IService _service;

        public ServiceConsumer(IService service)
        {
            _service = service;
        }

        public IService Service
        {
            get { return _service; }
        }
    }

    #endregion

    class Program
    {
        static void Main(string[] args)
        {
            IObjectRegistration serviceRegistration;
            IObjectContainer container = new ObjectContainer(false);
            container.Register<IService, Service1>().Return(out serviceRegistration);
            container.Register<IServiceConsumer, ServiceConsumer>();
            container.CommitRegistrations();

            var consumer = container.Resolve<IServiceConsumer>();
            Debug.Assert(consumer != null);
            Debug.Assert(consumer.Service != null);
            Debug.Assert(consumer.Service.Name == "Service1");

            container.Unregister(serviceRegistration);
            container.Register<IService, Service2>().Return(out serviceRegistration);
            container.CommitRegistrations();

            consumer = container.Resolve<IServiceConsumer>();
            Debug.Assert(consumer != null);
            Debug.Assert(consumer.Service != null);
            Debug.Assert(consumer.Service.Name == "Service2");

            Console.ReadLine();
        }
    }
}
